package com.community.zhihuishequ.view.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.AppCompatImageButton;
import android.support.v7.widget.AppCompatSpinner;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.community.zhihuishequ.R;

import java.lang.reflect.Field;
import java.util.List;

/**
 * Created by Administrator on 2017/12/22 0022.
 */

public abstract class BaseTabListActivity extends BaseActivity {

    protected FragmentManager fragmentManager;
    public TabLayout tabLayout;
    public ViewPager viewPager;
    public EditText etSearch;
    public AppCompatImageButton btnSearch;
    public AppCompatSpinner spinnerTimePicker;
    public Toolbar toolbar;
    public ArrayAdapter<String> adapter;
    public List<BaseListFragment> fragments;
    public String[] tabs;
    private TextView title;
    public int changeTimeType;
    public String changeEtSearch = "";

    private RelativeLayout bottomRl;
    private TextView bottomTv;
    private RelativeLayout rlSearch;
    protected LinearLayout searchAndTimeTypeLl;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        initListener();
    }

    private void initView() {
        fragmentManager = getSupportFragmentManager();
        setContentView(R.layout.activity_base_tab_list);
        toolbar = findViewById(R.id.toolBar);
        initActionBar(toolbar, true);

        tabLayout = findViewById(R.id.page_tab);
        viewPager = findViewById(R.id.page_main);
        etSearch = findViewById(R.id.et_search);
        btnSearch = findViewById(R.id.btn_search);
        spinnerTimePicker = findViewById(R.id.spinner_time_picker);
        bottomRl = findViewById(R.id.manager_bottom);
        bottomTv = findViewById(R.id.base_bottom);
        title = findViewById(R.id.tab_title);
        rlSearch = findViewById(R.id.rl_search);
        searchAndTimeTypeLl = findViewById(R.id.ll_search_and_timetype);
//        reflex(tabLayout,20);
    }

    /**
     * viewpage adapter的初始化
     *
     * @param fragments 需要添加fragment的列表
     * @param tabs      tablayout上的标题
     */
    public void initAdapter(List<BaseListFragment> fragments, String[] tabs) {
        this.fragments = fragments;
        this.tabs = tabs;
        BasePageAdapter pageAdapter = new BasePageAdapter(getSupportFragmentManager(), fragments, tabs);
        viewPager.setAdapter(pageAdapter);
        tabLayout.setupWithViewPager(viewPager);
        tabLayout.setTabMode(TabLayout.MODE_FIXED);
    }

    /**
     * 初始化下拉列表
     *
     * @param spinnerData 下拉列表的数据
     */
    public void initSpinnerData(String[] spinnerData) {
        adapter = new ArrayAdapter<String>(this, R.layout.spinner_item_alcohol_time_picker, spinnerData);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerTimePicker.setAdapter(adapter);
    }

    protected void initListener() {
        etSearch.setOnKeyListener(new View.OnKeyListener() {

            @Override

            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
                    // 先隐藏键盘
                    ((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE))
                            .hideSoftInputFromWindow(BaseTabListActivity.this.getCurrentFocus()
                                    .getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                    //进行搜索操作的方法，在该方法中可以加入mEditSearchUser的非空判断
                    search();
                }
                return false;
            }
        });

        rlSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                search();
            }
        });
    }

    public void search() {

    }

    /**
     * 获取当前显示的fragment
     *
     * @return
     */
    public Integer getViewPagePosition() {
        return viewPager.getCurrentItem();
    }

    /**
     * 设置搜索框上的提示文字
     *
     * @param hint
     */
    public void setSearchHint(String hint) {
        etSearch.setHint(hint);
    }

    /**
     * 日期选择下拉框消失
     */
    public void spinnerGone() {
        spinnerTimePicker.setVisibility(View.GONE);
    }

    /**
     * 底部总数条显示
     *
     * @param detail
     */
    public void setBottomTv(String detail) {
        bottomRl.setVisibility(View.VISIBLE);
        bottomTv.setText(detail);
    }

    /**
     * 设置标题
     *
     * @param detail
     */
    public void setTitle(String detail) {
        title.setText(detail);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
        }
        return super.onOptionsItemSelected(item);
    }

//    public static void reflex(final TabLayout tabLayout, final int width) {
//        //了解源码得知 线的宽度是根据 tabView的宽度来设置的
//        tabLayout.post(new Runnable() {
//            @Override
//            public void run() {
//                try {
//                    //拿到tabLayout的mTabStrip属性
//                    LinearLayout mTabStrip = (LinearLayout) tabLayout.getChildAt(0);
//
//                    int dp10 = BaseTabListFragment.DensityUtil.dip2px(tabLayout.getContext(), width);
//
//                    for (int i = 0; i < mTabStrip.getChildCount(); i++) {
//                        View tabView = mTabStrip.getChildAt(i);
//
//                        //拿到tabView的mTextView属性  tab的字数不固定一定用反射取mTextView
//                        Field mTextViewField = tabView.getClass().getDeclaredField("mTextView");
//                        mTextViewField.setAccessible(true);
//
//                        TextView mTextView = (TextView) mTextViewField.get(tabView);
//
//                        tabView.setPadding(0, 0, 0, 0);
//
//                        //因为我想要的效果是   字多宽线就多宽，所以测量mTextView的宽度
//                        int width = 0;
//                        width = mTextView.getWidth();
//                        if (width == 0) {
//                            mTextView.measure(0, 0);
//                            width = mTextView.getMeasuredWidth();
//                        }
//
//                        //设置tab左右间距为10dp  注意这里不能使用Padding 因为源码中线的宽度是根据 tabView的宽度来设置的
//                        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) tabView.getLayoutParams();
//                        params.width = width;
//                        params.leftMargin = dp10;
//                        params.rightMargin = dp10;
//                        tabView.setLayoutParams(params);
//
//                        tabView.invalidate();
//                    }
//
//                } catch (NoSuchFieldException e) {
//                    e.printStackTrace();
//                } catch (IllegalAccessException e) {
//                    e.printStackTrace();
//                }
//            }
//        });
//
//    }
}
